package com.ky.pro.java.sharding.ecommerce.util;

import java.util.ArrayList;
import java.util.List;

public class ListUtil {

    public static <T> List<List<T>> partListBalanced(List<T> list, int subListCnt) {
        int totalLen = list.size();

        int remainder = totalLen % subListCnt;
        int quotient = totalLen / subListCnt;

        int startIdx = 0;
        int endIdx;

        List<List<T>> subLists = new ArrayList<>();
        for (int i = 0; i < subListCnt; i++) {
            int subListLen = i < remainder ? quotient + 1 : quotient;
            endIdx = startIdx + subListLen;
            List<T> subList = list.subList(startIdx, endIdx);
            subLists.add(subList);
            startIdx = endIdx;
        }

        return subLists;

    }

}
